package ctrl

import (
	"im/service"
	"im/util"
	"net/http"
)

var contactService service.ContactService

type ContactArg struct {
    util.Page
    UserId int64 `json:"user_id" form:"user_id"`
    DstId int64 `json:"dst_id" form:"dst_id"`
}

func AddFriend(w http.ResponseWriter, r *http.Request) {
	var arg ContactArg
	util.Bind(r, &arg)
	err := contactService.AddFriend(arg.UserId, arg.DstId)
	if err != nil {
		util.Resp(w, -1, err.Error(), nil)
	} else {
		util.Resp(w, 0, "好友添加成功", nil)
	}
}

func LoadFriend(w http.ResponseWriter, r *http.Request){
	var arg ContactArg
	util.Bind(r, &arg)
	users := contactService.SearchFriend(arg.UserId)
	util.RespList(w, 0, users, len(users))
}

func JoinCommunity(w http.ResponseWriter, r *http.Request) {
    var arg ContactArg
    
    util.Bind(r, &arg)
    err := contactService.JoinCommunity(arg.UserId, arg.DstId)
    if err != nil {
        util.Resp(w, -1, err.Error(),nil)
    }
    AddGroup(arg.UserId, arg.DstId)
}

